import 'dart:async';

import 'package:flutter/foundation.dart';

class Debouncer {
  final Duration debounceDuration;
  Timer? _debounceTimer;

  Debouncer({required this.debounceDuration});

  void call(VoidCallback action) {
    if (_debounceTimer?.isActive ?? false) {
      _debounceTimer!.cancel();
    }
    _debounceTimer = Timer(debounceDuration, action);
  }
}

class Throttler {
  final Duration throttleDuration;
  DateTime? _lastCallTime;

  Throttler({required this.throttleDuration});

  void call(VoidCallback action) {
    final currentTime = DateTime.now();
    if (_lastCallTime == null || currentTime.difference(_lastCallTime!) >= throttleDuration) {
      action();
      _lastCallTime = currentTime;
    }
  }
}